<template>
  <!-- 头部 -->
  <header class="header">
    <!-- 头部的第一行 -->
    <div class="top">
      <div class="container">
        <div class="loginList">
          <p>随手换欢迎您！</p>
          <p v-if="isLogin"> {{ user.name }} <a href="/" @click="loginout"  >退出</a> </p>
          <p v-else>
            <span></span>
            <router-link to="/login">请登录</router-link>
            <router-link class="register" to="/register">免费注册</router-link>
          </p>
        </div>
        <div class="typeList">
          <a @click="trade">我的订单</a>
          <a  @click="toCart">我的购物车</a>
         
        </div>
      </div>
    </div>
    <!--头部第二行 搜索区域-->
    <div class="bottom">
      <h1 class="logoArea">
        <router-link class="logo" to="/home">
          <img src="./images/logo.png" alt="" />
        </router-link>
      </h1>
      
      <div class="searchArea" >
        <!-- <div class="tip"><img src="./images/tip.png" alt="" width="510px" height="34px"></div> -->
        <form action="###" class="searchForm">
        
          <div class="jvzhong">
          <input
            type="text"
            id="autocomplete"
            class="input-error input-xxlarge"
            v-model="keyword"
          />
          <div class="btn">
          <button
            class="sui-btn btn-xlarge btn-danger"
            type="button"
            @click="goSearch"
          >
            搜索
          </button>
          </div>
        </div>
        </form>
      </div>
    </div>
  </header>
</template>

<script>
import myhomeapi from '@/api/myhome';
export default {
  name: "Header",
  data(){
    return {
      keyword:'',
      isLogin:false,
      user:{
        name:'李氏'
      }
    }
  },
  created(){
    
    var token = localStorage.getItem("satoken")
    if(token){
      this.checkLogin()
    }
  },
  // mounted(){
  //   this.checkLogin()
  // },

  methods: {
     //订单页面
     trade(){
        this.$router.push('/trade')
      },
    //去购物车
    toCart(){
      this.$router.push('/shopcart')
    },

    //退出登录
    loginout(){
      localStorage.setItem("satoken",null)
      window.localStorage.clear()
      myhomeapi.loginout().then(res=>{
        this.isLogin=false
       
        // localStorage.setItem("satoken",undefined)
        window.localStorage.clear()
        this.$router.go({path:'/'})
      })
      

    },
      checkLogin(){
        console.log("=======checkLogin========")
        console.log(localStorage.getItem("satoken"))
        myhomeapi.checkInfo()
        .then(res=>{
          this.user.name=res.data.username
          this.isLogin=true
         
        }).catch(error=>{
          this.isLogin=false
        })
          
      },



    //搜索按钮的回调函数
    goSearch() {
      let locations = {
        name: "search",
        params: { keyword: this.keyword || undefined },
      };
      //确定路径当中有query参数
      if (this.$route.query.categoryName) {
        locations.query = this.$route.query;
      }

      this.$router.push(locations);
    },
  },
  mounted(){
    //通过全局事件总线清除关键字
    this.$bus.$on("clear",()=>{
      this.keyword=''
    })
  }
};
</script>

<style scoped lang="less">


.jvzhong{
  position:absolute ;
  
  
}
.tip{
  
  position: absolute;
  left:286px;
  top:114px
}
.header {
  // background: url('./images/huan.jpg');
  background-color: #8EC5FC;
background-image: linear-gradient(270deg, #8EC5FC 0%, #E0C3FC 24%, #f6e5e5 49%, #ffffff 75%, #ffffff 100%);

  & > .top {
    background-color: #eaeaea;
    height: 30px;
    line-height: 30px;

    .container {
      width: 1200px;
      margin: 0 auto;
      overflow: hidden;

      .loginList {
        float: left;

        p {
          float: left;
          margin-right: 10px;

          .register {
            border-left: 1px solid #b3aeae;
            padding: 0 5px;
            margin-left: 5px;
          }
        }
      }

      .typeList {
        float: right;

        a {
          padding: 0 10px;

          & + a {
            border-left: 1px solid #b3aeae;
          }
        }
      }
    }
  }
.btn{
  position: relative;
}
  & > .bottom {
    width: 1200px;
    margin: 0 auto;
    overflow: hidden;

    .logoArea {
      float: left;

      .logo {
        img {
          width: 175px;
          margin: 10px 18px;
        }
      }
    }

    .searchArea {
      
      margin-top: 35px;

      .searchForm {
        position: absolute;
        left: -735px;
        top: 3px;
        z-index: 999;
        float: right;
        position: relative;

        input {
          box-sizing: border-box;
          width: 490px;
          height: 32px;
          padding: 0px 4px;
          border: 2px solid #ea4a36;
          float: left;

          &:focus {
            outline: none;
          }
        }

        button {
          height: 32px;
          width: 68px;
          background-color: #ea4a36;
          border: none;
          color: #fff;
          float: left;
          cursor: pointer;
          position: absolute;
          left: 490px;
          height: 32px;
          &:focus {
            outline: none;
          }
        }
      }
    }
  }
}

</style>